capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local do "";
local crime "";
local output "";

/* 
Combine kid and sib 
*/


local exp "prworacombsib";
local range "1997(3)1997" ; /* "1997(3)2000" */
local firstyr 1997 ;	/* Kids turn 18 in 1996 */
local lastyr 2019 ;		/* Last year of crime data */
local lastearnyr 2019 ;


/* ******************************* */
/* **** DATA PREP FOR SIBLING **** */
/* ******************************* */

use "`crime'\prep2\crime_prworasib_remerge.dta", clear;

/* Sib DOB (in this dataset, "dob" is sib's DOB not age 18's DOB) */
gen sib_dobyy = year(dob) ;
drop if sib_dobyy < 1973 | sib_dobyy > 1990 ;

/* Reshape wide to get one observation per SSN */
forval i=1(1)20 { ;
	rename event_date`i' 	sib_event_date`i'_ ;
	rename event`i' 		sib_event`i'_ ;
	rename event_state`i' 	sib_event_state`i'_ ;
	rename day_of_week`i' 	sib_day_of_week`i'_ ;
} ;
foreach type in bk nobk vice { ;
	forval i=1(1)20 { ;
		capture rename `type'_event_date`i' 	sib_`type'_event_date`i'_ ;
		capture rename `type'_event`i' 			sib_`type'_event`i'_ ;
		capture rename `type'_event_state`i' 	sib_`type'_event_state`i'_ ;
	} ;
} ;
forval yr=1997(1)2019 { ;
	rename days_in_hprison`yr' 		sib_days_in_hinc`yr'_ ;
	rename days_in_prison`yr' 		sib_days_in_inca`yr'_ ;
	rename days_in_parole`yr' 		sib_days_in_par`yr'_ ;
	rename days_in_probation`yr' 	sib_days_in_prob`yr'_ ;
	rename days_in_jail`yr'			sib_days_in_jail`yr'_ ;
} ;
rename indiv_match sib_indiv_match ;

keep hun 
	sib_event_date* sib_event* sib_event_state* sib_day_of_week* 
	sib_bk_event_date* sib_bk_event* sib_bk_event_state* 
	sib_nobk_event_date* sib_nobk_event* sib_nobk_event_state* 
	sib_vice_event_date* sib_vice_event* sib_vice_event_state* 
	sib_days_in_* sib_indiv_match sib_dobyy sib_sex ;
bysort hun: gen num = _n ;
reshape wide 
	sib_event* sib_day_of_week* sib_bk_event* sib_nobk_event* sib_vice_event* 
	sib_days_in_* sib_indiv_match sib_dobyy sib_sex, i(hun) j(num) ;

forval sib = 1(1)7 { ;
	forval i=1(1)20 { ;
		rename sib_event_date`i'_`sib' 		sib`sib'_event_date`i' ;
		rename sib_event`i'_`sib' 			sib`sib'_event`i' ;
		rename sib_event_state`i'_`sib' 	sib`sib'_event_state`i' ;
		rename sib_day_of_week`i'_`sib' 	sib`sib'_day_of_week`i' ;
	} ;
	foreach type in bk nobk vice{ ;
		forval i=1(1)20 { ;
			capture rename sib_`type'_event_date`i'_`sib' 		sib`sib'_`type'_event_date`i' ;
			capture rename sib_`type'_event`i'_`sib' 			sib`sib'_`type'_event`i' ;
			capture rename sib_`type'_event_state`i'_`sib' 		sib`sib'_`type'_event_state`i' ;
		} ;
	} ;
	forval yr=1997(1)2019 { ;
		rename sib_days_in_hinc`yr'_`sib' sib`sib'_days_in_hinc`yr' ;
		rename sib_days_in_inca`yr'_`sib' sib`sib'_days_in_inca`yr' ;
		rename sib_days_in_par`yr'_`sib' sib`sib'_days_in_par`yr' ;
		rename sib_days_in_prob`yr'_`sib' sib`sib'_days_in_prob`yr' ;
		rename sib_days_in_jail`yr'_`sib' sib`sib'_days_in_jail`yr' ;
	} ;
	rename sib_dobyy`sib' 		sib`sib'_dobyy ;
	rename sib_sex`sib'			sib`sib'_sex ;
	rename sib_indiv_match`sib'	sib`sib'_indiv_match ;
	
	summ sib`sib'_dobyy ;
	local sib`sib'_yob_min = r(min) ;
	local sib`sib'_yob_max = r(max) ;
} ;

tempfile siblings ;
save `siblings' ;


/* ******************* */
/* **** DATA PREP **** */
/* ******************* */

local first 1996 ;
local last 2004 ;
local per_range "20" ; /*18(2)20*/
local thresh 0.8 ; 	/* how many years in the range have to have data for the state/cnty to be included */
include "`do'\crime_statecntyselect.do" ;

use "`crime'\prep2\crime_prwora_remerge.dta", clear;

forval i=1(1)9 { ;
	rename event_date0`i' event_date`i' ;
	rename event0`i' event`i' ;
	rename day_of_week0`i' day_of_week`i' ;
	rename event_state0`i' event_state`i' ;
	
	foreach type in bk nobk vice { ;
		rename `type'_event_date0`i' 	`type'_event_date`i' ;
		rename `type'_event0`i' 		`type'_event`i' ;
		rename `type'_event_state0`i'	`type'_event_state`i' ;
	} ;
} ;

forval i=1(1)20 { ;
	rename event_date`i' 	kid_event_date`i' ;
	rename event`i' 		kid_event`i' ;
	rename event_state`i' 	kid_event_state`i' ;
	rename day_of_week`i' 	kid_day_of_week`i' ;
} ;
foreach type in bk nobk vice { ;
	forval i=1(1)20 { ;
		rename `type'_event_date`i' 	kid_`type'_event_date`i' ;
		rename `type'_event`i' 		kid_`type'_event`i' ;
		rename `type'_event_state`i' 	kid_`type'_event_state`i' ;
	} ;
} ;
forval yr=1997(1)2019 { ;
	rename days_in_hprison`yr' 		kid_days_in_hinc`yr' ;
	rename days_in_prison`yr'		kid_days_in_inca`yr' ;
	rename days_in_parole`yr' 		kid_days_in_par`yr' ;
	rename days_in_probation`yr' 	kid_days_in_prob`yr' ;
	rename days_in_jail`yr'			kid_days_in_jail`yr' ;
} ;
rename indiv_match kid_indiv_match ;
gen kid_dobyy = year(dob) ;
summ kid_dobyy ;
local kid_yob_min = r(min) ;
local kid_yob_max = r(max) ;
replace kid_dobyy = `=`kid_yob_min'+1' if kid_dobyy == `kid_yob_min' ;
replace kid_dobyy = `=`kid_yob_max'-1' if kid_dobyy == `kid_yob_max' ;
summ kid_dobyy ;
local kid_yob_min = r(min) ;
local kid_yob_max = r(max) ;

merge 1:1 hun using `siblings' ;
drop if _merge == 2 ;
gen byte has_sib = (_merge == 3) ;
tab has_sib, m ;
drop _merge ;



/* Diagnosis (only ever used as factor variable, so replace missing values with 999) */
tab n_diag1cat, m;
gen byte n_diag1_infec=(n_diag1cat==1);
gen byte n_diag1_neo=(n_diag1cat==2);
gen byte n_diag1_endo=(n_diag1cat==3);
gen byte n_diag1_blood=(n_diag1cat==4);
gen byte n_diag1_mental=(n_diag1cat==5);
gen byte n_diag1_nerv=(n_diag1cat==6);
gen byte n_diag1_sense=(n_diag1cat==7);
gen byte n_diag1_circ=(n_diag1cat==8);
gen byte n_diag1_resp=(n_diag1cat==9);
gen byte n_diag1_dig=(n_diag1cat==10);
gen byte n_diag1_gu=(n_diag1cat==11);
gen byte n_diag1_preg=(n_diag1cat==12);
gen byte n_diag1_skin=(n_diag1cat==13);
gen byte n_diag1_musc=(n_diag1cat==14);
gen byte n_diag1_cong=(n_diag1cat==15);
gen byte n_diag1_nat=(n_diag1cat==16);
gen byte n_diag1_ill=(n_diag1cat==17);
gen byte n_diag1_inj=(n_diag1cat==18);
gen byte n_diag1_none=(n_diag1cat==.);
replace n_diag1cat = 999 if n_diag1cat == . ;

/* Family structure */
tab toa, m;
gen byte n_fam_singmom=	(toa=="DM" | toa=="BM");
gen byte n_fam_nopar=	(toa=="DC" | toa=="BC");
tab n_fam_singmom, m;
tab n_fam_nopar, m;

/* Male */
tab n_male, m;

/* Severity */
tab dibmdr, m;
gen byte n_dibmdr_miss=(dibmdr=="");
gen byte n_dibmdr_mip=(dibmdr=="3");
gen byte n_dibmdr_mine=(dibmdr=="5" | dibmdr=="7");
gen byte n_dibmdr_mie=(!n_dibmdr_miss & !n_dibmdr_mip & !n_dibmdr_mine);
tab n_dibmdr_mie, m;
tab n_dibmdr_mip, m;
tab n_dibmdr_mine, m;

/* Age at entry (only ever used as factor variable, so replace missing values with 999) */
tab n_firstage, m;
replace n_firstage = 999 if n_firstage == . ;
tab n_firstage, m ;

/* Parent earnings and household income */
local earncut = 5000 ;

order n_hhearns1996_*  n_combpay_* n_hhmbayr1996_* n_hhcombpay1996_* n_sibpayall_* ;
forval yr=1990(1)2012 { ;
	gen n_inchh1996_`yr' = n_hhearns1996_`yr' + n_combpay_`yr' + n_hhmbayr1996_`yr' + n_hhcombpay1996_`yr' + n_sibpayall_`yr' if n_hhearns1996_`yr' != . ;
} ;
forval yr=1990(1)2012 { ;
	gen n_inchh_`yr' = n_hhearns_`yr' + n_combpay_`yr' + n_hhmbayr_`yr' + n_hhcombpay_`yr' + n_sibpayall_`yr' if n_hhearns_`yr' != . ;
} ;
summ n_inchh* ;

egen n_earnhh9702=rowmean(n_hhearns_1997-n_hhearns_2002) ;
egen n_earnhh9712=rowmean(n_hhearns_1997-n_hhearns_2012) ;
egen n_earnhhpre9095=rowmean(n_hhearns_1990-n_hhearns_1995) ;

egen n_inchh9702=rowmean(n_inchh_1997-n_inchh_2002) ;
egen n_inchh9712=rowmean(n_inchh_1997-n_inchh_2012) ;
egen n_inchhpre9095=rowmean(n_inchh_1990-n_inchh_1995) ;

forval yr=1990(1)2012 { ;
	gen byte temp`yr' = (n_hhearns_`yr' > `earncut') ;
	replace temp`yr' = . if n_hhearns_`yr' == . ;
} ;
egen n_emphhpost9702=rowmean(temp1997-temp2002) ;
egen n_emphhpost9712=rowmean(temp1997-temp2012) ;
egen n_emphhpre9095=rowmean(temp1990-temp1995) ;
drop temp* ;

/* Create quantiles of pre-treatment parent earnings and replace missing values */
xtile n_earnhhbins = n_earnhhpre9095, nq(10) ;
tab n_earnhhbins, m ;
replace n_earnhhbins = 99 if n_earnhhbins == . ;
tab n_earnhhbins, m ;

/* First stage variables: on SSI as kid, unfavorable age 18 */
gen byte n_age18unfav=(age18_final_con==0);
egen n_onssi1922 = rowmean(n_onssi_1997-n_onssi_2000) ;
summ n_age18unfav n_onssi1922 ;


/* ********************************* */
/* **** CREATE RUNNING VARIABLE **** */
/* ********************************* */
gen dobdd_stata	=mdy(month(dob),day(dob),year(dob)+18); 
gen dobdd_run	=dobdd_stata-mdy(08,22,1996);

gen dobwk_stata	=wofd(mdy(month(dob),day(dob),year(dob)+18));
gen dobwk_run	=dobwk_stata-wofd(mdy(08,22,1996));

gen dobmo_stata	=ym(year(dob)+18,month(dob)) ;
gen dobmo_run	=dobmo_stata-mofd(mdy(08,22,1996)) ;


/* ************************** */
/* **** SAMPLE SELECTION **** */
/* ************************** */

keep if year(first_pay_dte)<1996 ;
count;


/* ********************************************************************** */
/* **** CREATE CRIME VARIABLES USING DIFFERENT GEO/YEAR DENOMINATORS **** */
/* ********************************************************************** */

/* Determine county-years that make up denominator */
rename pdscc pdscc_full ;
gen pdscc = substr(pdscc_full,1,5) ;
gen temp_st = substr(pdscc_full,1,2) ;
tab temp_st, m ;
drop temp_st pdscc_full ;

merge m:1 pdscc using `cnty_year', keepusing(keep_* cnty_*) ;
drop if _merge == 2 ;
drop _merge ;
foreach var of varlist keep* { ;
	replace `var' = 0 if `var' == . ;
} ;

summ keep* ;


/* ******************************** */
/* **** DEFINE CRIME VARIABLES **** */
/* ******************************** */

local char_cond 	2 ;
local drug_cond 	3 ;
local traf_cond     4 ;
local fel_cond		5 ;
local genr_cond		6 ;
local nogn_cond  	7 ;
local prop_cond 	8 ;
local pord_cond     9 ;
local viol_cond 	10 ;
local wknd_cond		11 ;
local fcvn_cond		12 ;
local acvn_cond     13 ;
local inca_cond 	14 ;
local par_cond 		15 ;
local prob_cond 	16 ;

local dgsl_cond 1 ;
local dgot_cond 2 ;
local dgps_cond 3 ;
local polq_cond	4 ;
local poot_cond	5 ;
local povc_cond	6 ;
local povl_cond	7 ;
local powp_cond	8 ;
local prbg_cond 9 ;
local prfd_cond 10 ;
local prtf_cond 11 ;
local vrob_cond 12 ;
local vlct_cond 13 ;
local vlpa_cond 14 ;
local vlpr_cond 15 ;
local hinc_cond 16 ;

local vcch_cond 1 ;
local vcgb_cond 2 ;
local vcpd_cond 3 ;
local vcpp_cond 4 ;
local vcps_cond 5 ;

local nias_cond 1 ; 
local nide_cond 2 ; 
local nidc_cond 3 ; 
local nidp_cond 4 ; 
local nidu_cond 5 ; 
local nilq_cond 6 ; 
local niob_cond 7 ; 
local nisx_cond 8 ; 
local nitf_cond 9 ; 
local nitp_cond 10 ; 
local niwp_cond 11 ;	

local any_abbr		"any" ;
local char_abbr 	"2c" ;
local drug_abbr 	"3cd" ;
local traf_abbr 	"4ct" ;
local fel_abbr  	"5cf" ;
local genr_abbr 	"6cg" ;
local nogn_abbr     "7cn" ;
local prop_abbr 	"8cp" ;
local pord_abbr     "9co" ;
local viol_abbr 	"10cv" ;
local wknd_abbr 	"11cw" ;
local fcvn_abbr 	"12fc" ;
local acvn_abbr     "13fm" ;
local inca_abbr		"14ie" ;	
local par_abbr		"15pa" ;
local prob_abbr		"16pr" ;
local ipp_abbr		"ipp" ;
local jail_abbr		"jail" ;

foreach stub in 
	dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc  
	nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp
	vcch vcgb vcpd vcpp vcps
	{ ;
		local `stub'_abbr "``stub'_cond'`stub'" ;
	} ;

local type_list_main "char genr nogn drug viol prop pord fel fcvn acvn inca" ; 
local type_list_bk "dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc" ;
local type_list_nobk "nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp" ;
local type_list_vice "vcch vcgb vcpd vcpp vcps" ;
local type_list "`type_list_main' `type_list_bk' `type_list_nobk' `type_list_vice' " ;

foreach stub in `type_list_main' { ;
	local `stub'_event "event" ;
} ;
foreach stub in `type_list_bk' { ;
	local `stub'_event "bk_event" ;
} ;
foreach stub in `type_list_nobk' { ;
	local `stub'_event "nobk_event" ;
} ;
foreach stub in `type_list_vice' { ;
	local `stub'_event "vice_event" ;
} ;


/* There aren't as many vice variables as the other ones, so need to fill in */
forval i = 13(1)20 { ;
	capture gen kid_vice_event`i' = . ;
	capture gen kid_vice_event_date`i' = . ;
	
	forval sib = 1(1)7 { ;
		capture gen sib`sib'_vice_event`i' = . ;
		capture gen sib`sib'_vice_event_date`i' = . ;
	} ;
} ;

/* Variable formats */
forval i=1(1)20 { ;
	tostring(kid_event`i'), replace format(%18.0g) ;
	forval j = 3(1)9 { ;
		replace kid_event`i' = subinstr(kid_event`i',"`j'","2",.) ;
	} ;
	
	forval sib = 1(1)7 { ;
		tostring(sib`sib'_event`i'), replace format(%18.0g) ;
		forval j = 3(1)9 { ;
			replace sib`sib'_event`i' = subinstr(sib`sib'_event`i',"`j'","2",.) ;
		} ;
	} ;
} ;

foreach class in bk nobk vice { ;
	forval i=1(1)20 { ;
		recast double kid_`class'_event`i' ;
		tostring(kid_`class'_event`i'), replace format(%19.0g) ;
		forval j = 3(1)9 { ;
			replace kid_`class'_event`i' = subinstr(kid_`class'_event`i',"`j'","2",.) ;
		} ;
		
		forval sib = 1(1)7 { ;
			recast double sib`sib'_`class'_event`i' ;
			tostring(sib`sib'_`class'_event`i'), replace format(%19.0g) ;
			forval j = 3(1)9 { ;
				replace sib`sib'_`class'_event`i' = subinstr(sib`sib'_`class'_event`i',"`j'","2",.) ;
			} ;
		} ;
	} ;
} ;


/* Create crime variables */
gen		 kid_crime1 = kid_indiv_match if keep_`firstyr'post_any == 1 ;
gen byte sib_crime1 = 0 if has_sib == 1 & keep_`firstyr'post_any == 1 ;
forval sib = 1(1)7 { ;
	replace  sib_crime1 = 1 if sib_crime1 != . & sib`sib'_indiv_match == 1 ;
	drop sib`sib'_indiv_match ;
} ;

gen byte kid_crime2 = (kid_event_date1 != .) if keep_`firstyr'post_any == 1 ;
gen byte sib_crime2 = 0 if has_sib == 1 & keep_`firstyr'post_any == 1 ;
forval sib = 1(1)7 { ;
	replace  sib_crime2 = 1 if sib_crime2 != . & sib`sib'_event_date1 != . ;
} ;

forval i=1(1)2 { ;
	gen byte any_crime`i' = (kid_crime`i' == 1 | sib_crime`i' == 1) if keep_`firstyr'post_any == 1 ;
} ;



local sib_list "sib1 sib2 sib3 sib4 sib5 sib6 sib7" ;

/* **** GENERIC **** */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
	
		/* Indicator */
		foreach person in kid `sib_list' { ;

			gen 		`person'_crime2any_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace `person'_crime2any_`start'_`end' = 1 
					if (keep_`start'_`end'_any == 1)
					& year(`person'_event_date`i' ) >= `start' & year(`person'_event_date`i' ) <= `end' ;
				} ;
		} ; /* end person */
		
		gen byte any_crime2any_`start'_`end' = (kid_crime2any_`start'_`end' == 1) if kid_crime2any_`start'_`end' != . ;
		forval sib = 1(1)7 { ;
			replace any_crime2any_`start'_`end' = 1 if any_crime2any_`start'_`end' !=. & sib`sib'_crime2any_`start'_`end' == 1 ;
		} ;
		
		foreach person in kid `sib_list' { ;
			drop 		`person'_crime2any_`start'_`end' ;
		} ; 
		
		/* Total number */
		foreach person in kid `sib_list' { ;
		
			gen 		`person'_crime2anyt_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace `person'_crime2anyt_`start'_`end' = `person'_crime2anyt_`start'_`end' + 1 
					if (keep_`start'_`end'_any == 1)
					& year(`person'_event_date`i' ) >= `start' & year(`person'_event_date`i' ) <= `end' ;
				} ;
		} ;
		
		gen any_crime2anyt_`start'_`end' = kid_crime2anyt_`start'_`end' if kid_crime2anyt_`start'_`end' != . ;
		forval sib = 1(1)7 { ;
			replace any_crime2anyt_`start'_`end' = any_crime2anyt_`start'_`end' + sib`sib'_crime2anyt_`start'_`end' 
				if any_crime2anyt_`start'_`end' !=. & sib`sib'_crime2anyt_`start'_`end' !=. ;
		} ;
		
		foreach person in kid `sib_list' { ;
			drop 		`person'_crime2anyt_`start'_`end' ;
		} ; 
		
		/* Cumulative */
		forval yr = `start'(4)`end' { ;
		
			foreach person in kid `sib_list' { ;

				gen 		c`person'_crime2any`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_any == 1) ;
				forval i=1(1)20 { ;
					replace c`person'_crime2any`yr'_`start'_`end' = 1
						if (keep_`start'_`end'_any == 1)
						& year(`person'_event_date`i' ) >= `start' & year(`person'_event_date`i' ) <= `yr' ;
					} ;
				} ;
			
			gen byte ca_crime2any`yr'_`start'_`end' = (ckid_crime2any`yr'_`start'_`end' == 1) if ckid_crime2any`yr'_`start'_`end' != . ;
			forval sib = 1(1)7 { ;
				replace ca_crime2any`yr'_`start'_`end' = 1 if ca_crime2any`yr'_`start'_`end' !=. & csib`sib'_crime2any`yr'_`start'_`end' == 1 ;
			} ;
			
			foreach person in kid `sib_list' { ;
				drop 		c`person'_crime2any`yr'_`start'_`end' ;
			} ; 
		
		} ; /* end yr */
			
		forval yr = `start'(4)`end' { ;
		
			foreach person in kid `sib_list' { ;
			
				gen 		c`person'_crime2anyt`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_any == 1) ;
				forval i=1(1)20 { ;
					replace c`person'_crime2anyt`yr'_`start'_`end' = c`person'_crime2anyt`yr'_`start'_`end' + 1 
						if (keep_`start'_`end'_any == 1)
						& year(`person'_event_date`i' ) >= `start' & year(`person'_event_date`i' ) <= `yr' ;
					} ;
				} ; /* end person */
				
				gen ca_crime2anyt`yr'_`start'_`end' = ckid_crime2anyt`yr'_`start'_`end' if ckid_crime2anyt`yr'_`start'_`end' != . ;
				forval sib = 1(1)7 { ;
					replace ca_crime2anyt`yr'_`start'_`end' = ca_crime2anyt`yr'_`start'_`end' + csib`sib'_crime2anyt`yr'_`start'_`end' 
						if ca_crime2anyt`yr'_`start'_`end' !=. & csib`sib'_crime2anyt`yr'_`start'_`end' !=. ;
				} ;
				
				foreach person in kid `sib_list' { ;
						drop 		c`person'_crime2anyt`yr'_`start'_`end' ;
					} ; 
					
			} ; /* end yr */
				
	} ;	/* end end */
} ;	/* end start */


/* **** SPECIFIC TYPES OF CRIME **** */
foreach type in `type_list' { ;
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
		
		/* Indicator */
		foreach person in kid `sib_list' { ;

			gen 		`person'_crime2`type'_`start'_`end'= 0 
					if (keep_`start'_`end'_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace `person'_crime2`type'_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& year(`person'_``type'_event'_date`i' ) >= `start' & year(`person'_``type'_event'_date`i' ) <= `end' 
					& substr(`person'_``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
		} ; /* end person */
		
		gen byte any_crime2`type'_`start'_`end' = (kid_crime2`type'_`start'_`end' == 1) if kid_crime2`type'_`start'_`end' != . ;
		forval sib = 1(1)7 { ;
			replace any_crime2`type'_`start'_`end' = 1 if any_crime2`type'_`start'_`end' !=. & sib`sib'_crime2`type'_`start'_`end' == 1 ;
		} ;
		
		foreach person in kid `sib_list' { ;
				drop 		`person'_crime2`type'_`start'_`end' ;
			} ; 
		
		/* Total number */
		foreach person in kid `sib_list' { ;
		
			gen 		`person'_crime2`type't_`start'_`end'= 0 
					if (keep_`start'_`end'_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace `person'_crime2`type't_`start'_`end' = `person'_crime2`type't_`start'_`end' + 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& year(`person'_``type'_event'_date`i' ) >= `start' & year(`person'_``type'_event'_date`i' ) <= `end' 
					& substr(`person'_``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
		} ;
		
		gen any_crime2`type't_`start'_`end' = kid_crime2`type't_`start'_`end' if kid_crime2`type't_`start'_`end' != . ;
		forval sib = 1(1)7 { ;
			replace any_crime2`type't_`start'_`end' = any_crime2`type't_`start'_`end' + sib`sib'_crime2`type't_`start'_`end' 
				if any_crime2`type't_`start'_`end' !=. & sib`sib'_crime2`type't_`start'_`end' !=. ;
		} ;
		
		foreach person in kid `sib_list' { ;
				drop 		`person'_crime2`type't_`start'_`end' ;
			} ; 
		
		/* Cumulative */
		forval yr = `start'(4)`end' { ;
		
			foreach person in kid `sib_list' { ;

				gen 		c`person'_crime2`type'`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c`person'_crime2`type'`yr'_`start'_`end' = 1
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(`person'_``type'_event'_date`i' ) >= `start' & year(`person'_``type'_event'_date`i' ) <= `yr' 
						& substr(`person'_``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ;
			
			gen byte ca_crime2`type'`yr'_`start'_`end' = (ckid_crime2`type'`yr'_`start'_`end' == 1) if ckid_crime2`type'`yr'_`start'_`end' != . ;
			forval sib = 1(1)7 { ;
				replace ca_crime2`type'`yr'_`start'_`end' = 1 if ca_crime2`type'`yr'_`start'_`end' !=. & csib`sib'_crime2`type'`yr'_`start'_`end' == 1 ;
			} ;
			
			foreach person in kid `sib_list' { ;
				drop 		c`person'_crime2`type'`yr'_`start'_`end' ;
			} ; 
			
		} ; /* end yr */
			
		forval yr = `start'(4)`end' { ;
		
			foreach person in kid `sib_list' { ;
			
				gen 		c`person'_crime2`type't`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c`person'_crime2`type't`yr'_`start'_`end' = c`person'_crime2`type't`yr'_`start'_`end' + 1 
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(`person'_``type'_event'_date`i' ) <= `yr' 
						& substr(`person'_``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ; /* end person */
				
				gen ca_crime2`type't`yr'_`start'_`end' = ckid_crime2`type't`yr'_`start'_`end' if ckid_crime2`type't`yr'_`start'_`end' != . ;
				forval sib = 1(1)7 { ;
					replace ca_crime2`type't`yr'_`start'_`end' = ca_crime2`type't`yr'_`start'_`end' + csib`sib'_crime2`type't`yr'_`start'_`end' 
						if ca_crime2`type't`yr'_`start'_`end' !=. & csib`sib'_crime2`type't`yr'_`start'_`end' !=. ;
				} ;
				
			foreach person in kid `sib_list' { ;
				drop 		c`person'_crime2`type't`yr'_`start'_`end' ;
			} ; 
		
			} ; /* end yr */
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* type */

	forval start = `range' { ;
		foreach end in `list_`start'' { ;
		
			foreach person in kid `sib_list' { ;

				gen 		`person'_crime2vnrb_`start'_`end'= 0 
						if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace `person'_crime2vnrb_`start'_`end' = 1 
						if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1)
						& year(`person'_`viol_event'_date`i' ) >= `start' & year(`person'_`viol_event'_date`i' ) <= `end' 
						& substr(`person'_`viol_event'`i',`viol_cond',1) == "2" & substr(`person'_`vrob_event'`i',`vrob_cond',1) != "2" ;
					} ;
			} ; /* end person */
			
			gen byte any_crime2vnrb_`start'_`end' = (kid_crime2vnrb_`start'_`end' == 1) if kid_crime2vnrb_`start'_`end' != . ;
			forval sib = 1(1)7 { ;
				replace any_crime2vnrb_`start'_`end' = 1 if any_crime2vnrb_`start'_`end' !=. & sib`sib'_crime2vnrb_`start'_`end' == 1 ;
			} ;
			
			foreach person in kid `sib_list' { ;
					drop 		`person'_crime2vnrb_`start'_`end' ;
				} ; 
		} ;
	} ;	



/* Incarceration, parole, or probation FLOW */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
	
		/* Indicator */
		foreach person in kid `sib_list' { ;

			gen 		`person'_crime2ipp_`start'_`end'= 0 
					if (keep_`start'_`end'_ipp == 1) ;
			forval i=1(1)20 { ;
				replace `person'_crime2ipp_`start'_`end' = 1 
					if (keep_`start'_`end'_ipp == 1)
					& year(`person'_event_date`i' ) >= `start' & year(`person'_event_date`i' ) <= `end' 
					& (substr(`person'_event`i',`inca_cond',1) == "2" | substr(`person'_event`i',`par_cond',1) == "2" | substr(`person'_event`i',`prob_cond',1) == "2") ;
				} ;
		} ; /* end person */
		
		gen byte any_crime2ipp_`start'_`end' = (kid_crime2ipp_`start'_`end' == 1) if kid_crime2ipp_`start'_`end' != . ;
		forval sib = 1(1)7 { ;
			replace any_crime2ipp_`start'_`end' = 1 if any_crime2ipp_`start'_`end' !=. & sib`sib'_crime2ipp_`start'_`end' == 1 ;
		} ;
		
		foreach person in kid `sib_list' { ;
				drop 		`person'_crime2ipp_`start'_`end' ;
		} ; 
		
		/* Total number */
		foreach person in kid `sib_list' { ;
		
			gen 		`person'_crime2ippt_`start'_`end'= 0 
					if (keep_`start'_`end'_ipp == 1) ;
			forval i=1(1)20 { ;
				replace `person'_crime2ippt_`start'_`end' = `person'_crime2ippt_`start'_`end' + 1 
					if (keep_`start'_`end'_ipp == 1)
					& year(`person'_event_date`i' ) >= `start' & year(`person'_event_date`i' ) <= `end' 
					& (substr(`person'_event`i',`inca_cond',1) == "2" | substr(`person'_event`i',`par_cond',1) == "2" | substr(`person'_event`i',`prob_cond',1) == "2") ;
				} ;
		} ;
		
		gen any_crime2ippt_`start'_`end' = kid_crime2ippt_`start'_`end' if kid_crime2ippt_`start'_`end' != . ;
		forval sib = 1(1)7 { ;
			replace any_crime2ippt_`start'_`end' = any_crime2ippt_`start'_`end' + sib`sib'_crime2ippt_`start'_`end' 
				if any_crime2ippt_`start'_`end' !=. & sib`sib'_crime2ippt_`start'_`end' !=. ;
		} ;
		
		foreach person in kid `sib_list' { ;
				drop 		`person'_crime2ippt_`start'_`end' ;
		} ; 
		
		/* Cumulative */
		forval yr = `start'(4)`end' { ;
		
			foreach person in kid `sib_list' { ;

				gen 		c`person'_crime2ipp`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_ipp == 1) ;
				forval i=1(1)20 { ;
					replace c`person'_crime2ipp`yr'_`start'_`end' = 1
						if (keep_`start'_`end'_ipp == 1)
						& year(`person'_event_date`i' ) <= `yr' 
						& (substr(`person'_event`i',`inca_cond',1) == "2" | substr(`person'_event`i',`par_cond',1) == "2" | substr(`person'_event`i',`prob_cond',1) == "2") ;
					} ;
				} ;
			
			gen byte ca_crime2ipp`yr'_`start'_`end' = (ckid_crime2ipp`yr'_`start'_`end' == 1) if ckid_crime2ipp`yr'_`start'_`end' != . ;
			forval sib = 1(1)7 { ;
				replace ca_crime2ipp`yr'_`start'_`end' = 1 if ca_crime2ipp`yr'_`start'_`end' !=. & csib`sib'_crime2ipp`yr'_`start'_`end' == 1 ;
			} ;
			
			foreach person in kid `sib_list' { ;
				drop 		c`person'_crime2ipp`yr'_`start'_`end' ;
			} ; 
			
		} ; /* end yr */
			
		forval yr = `start'(4)`end' { ;
		
			foreach person in kid `sib_list' { ;
			
				gen 		c`person'_crime2ippt`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_ipp == 1) ;
				forval i=1(1)20 { ;
					replace c`person'_crime2ippt`yr'_`start'_`end' = c`person'_crime2ippt`yr'_`start'_`end' + 1 
						if (keep_`start'_`end'_ipp == 1)
						& year(`person'_event_date`i' ) <= `yr' 
						& (substr(`person'_event`i',`inca_cond',1) == "2" | substr(`person'_event`i',`par_cond',1) == "2" | substr(`person'_event`i',`prob_cond',1) == "2") ;
					} ;
				} ; /* end person */
				
				gen ca_crime2ippt`yr'_`start'_`end' = ckid_crime2ippt`yr'_`start'_`end' if ckid_crime2ippt`yr'_`start'_`end' != . ;
				forval sib = 1(1)7 { ;
					replace ca_crime2ippt`yr'_`start'_`end' = ca_crime2ippt`yr'_`start'_`end' + csib`sib'_crime2ippt`yr'_`start'_`end' 
						if ca_crime2ippt`yr'_`start'_`end' !=. & csib`sib'_crime2ippt`yr'_`start'_`end' !=. ;
				} ;
				
				foreach person in kid `sib_list' { ;
					drop 		c`person'_crime2ippt`yr'_`start'_`end' ;
				} ; 
		
			} ; /* end yr */
		
		} ;	/* end end */
} ;	/* end start */


/* Length of IPP spells / indicator for any IPP */
local full_hinc "hprison" ;
local full_inca "prison" ;
local full_par  "parole" ;
local full_prob "probation" ;
local full_ipp 	"ipp" ;
local full_jail "jail" ;

local correct_list "hinc inca par prob ipp jail" ; 

foreach person in kid `sib_list' { ;
	forval yr = 1997(1)2019 { ;
		gen `person'_days_in_ipp`yr' = `person'_days_in_inca`yr' + `person'_days_in_par`yr' + `person'_days_in_prob`yr' ;
	} ;
} ;

foreach type in `correct_list' { ;
	
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			
			foreach person in kid `sib_list' { ;
				egen 		`person'_crime2s`type'_`start'_`end' = rowmean(`person'_days_in_`type'`start'-`person'_days_in_`type'`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;	
			} ;
			
			gen any_crime2s`type'_`start'_`end' = kid_crime2s`type'_`start'_`end' if kid_crime2s`type'_`start'_`end' != . ;
			forval sib = 1(1)7 { ;
				replace any_crime2s`type'_`start'_`end' = any_crime2s`type'_`start'_`end' + sib`sib'_crime2s`type'_`start'_`end' 
					if any_crime2s`type'_`start'_`end' !=. & sib`sib'_crime2s`type'_`start'_`end' !=. ;
				} ;
				
			foreach person in kid `sib_list' { ;
				drop 		`person'_crime2s`type'_`start'_`end' ;
			} ; 
				

			/* cumulative */
			forval yr = `start'(4)`end' { ;
			
				foreach person in kid `sib_list' { ;
					egen c`person'_crime2s`type'`yr'_`start'_`end' = rowtotal(`person'_days_in_`type'1997-`person'_days_in_`type'`yr') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				} ;
				
				gen ca_crime2s`type'`yr'_`start'_`end' = ckid_crime2s`type'`yr'_`start'_`end' if ckid_crime2s`type'`yr'_`start'_`end' != . ;
				forval sib = 1(1)7 { ;
					replace ca_crime2s`type'`yr'_`start'_`end' = ca_crime2s`type'`yr'_`start'_`end' + csib`sib'_crime2s`type'`yr'_`start'_`end' 
						if ca_crime2s`type'`yr'_`start'_`end' !=. & csib`sib'_crime2s`type'`yr'_`start'_`end' !=. ;
					} ;
					
				foreach person in kid `sib_list' { ;
					drop 		c`person'_crime2s`type'`yr'_`start'_`end' ;
				} ; 
					
			} ; /* end yr */
				
		} ;	/* end end */
	} ;	/* end start */

} ; /* end type */


/* change denominator to that year rather than entire 2000-2016 period */
foreach type in `correct_list' { ;
	forval i=`firstyr'(1)`lastyr' { ;
		
		foreach person in kid `sib_list' { ;
			gen `person'_yrcrime2s`type'_`i' = `person'_days_in_`type'`i' if (keep_yr`i'_`type' == 1) ;
		} ;
		
		gen any_yrcrime2s`type'_`i' = kid_yrcrime2s`type'_`i' if kid_yrcrime2s`type'_`i' != . ;
			forval sib = 1(1)7 { ;
				replace any_yrcrime2s`type'_`i' = any_yrcrime2s`type'_`i' + sib`sib'_yrcrime2s`type'_`i'
					if any_yrcrime2s`type'_`i' !=. & sib`sib'_yrcrime2s`type'_`i' !=. ;
				} ;
				
		foreach person in kid `sib_list' { ;
			drop 		`person'_yrcrime2s`type'_`i' ;
		} ; 
		
	} ;
} ;

/* new "any" stock variables */
foreach type in `correct_list' { ;
	foreach person in kid `sib_list' { ;
		forval yr = 1997(1)2019 { ;
			gen byte `person'_any_`type'`yr' = (`person'_days_in_`type'`yr' > 0) ;
			replace  `person'_any_`type'`yr' = . if `person'_days_in_`type'`yr' == . ;
		} ;
	} ;
} ;

foreach type in `correct_list' { ;
	
	forval start = `range' { ;
			foreach end in `list_`start'' { ;
			
				foreach person in kid `sib_list' { ;
					egen 		`person'_crime2a`type'_`start'_`end' = rowmean(`person'_any_`type'`start'-`person'_any_`type'`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;	
				} ;
				
				egen any_crime2a`type'_`start'_`end' = rowmax(kid_crime2a`type'_`start'_`end' sib*_crime2a`type'_`start'_`end') ;
				
				foreach person in kid `sib_list' { ;
					drop 		`person'_crime2a`type'_`start'_`end' ;
				} ; 
			
		
				foreach person in kid `sib_list' { ;
					egen 		`person'_crime2c`type'_`start'_`end' = rowmax(`person'_any_`type'`start'-`person'_any_`type'`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				} ;
				
				egen any_crime2c`type'_`start'_`end' = rowmax(kid_crime2c`type'_`start'_`end' sib*_crime2c`type'_`start'_`end') ;
				
				foreach person in kid `sib_list' { ;
					drop 		`person'_crime2c`type'_`start'_`end' ;
				} ; 
				
				/* cumulative */
				forval yr = `=`end'-(4*4)'(4)`end' { ;
					
					foreach person in kid `sib_list' { ;
						egen c`person'_crime2c`type'`yr'_`start'_`end' = rowmax(`person'_any_`type'1997-`person'_any_`type'`yr') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
					} ;
					
					egen cany_crime2c`type'`yr'_`start'_`end' = rowmax(ckid_crime2c`type'`yr'_`start'_`end' csib*_crime2c`type'`yr'_`start'_`end') ;
				
					foreach person in kid `sib_list' { ;
						drop 		c`person'_crime2c`type'`yr'_`start'_`end' ;
					} ; 
				
				} ;
				
			} ;	/* end end */
	} ;	/* end start */

} ; /* end type */

foreach type in `correct_list' { ;
	forval i=`firstyr'(1)`lastyr' { ;
	
		foreach person in kid `sib_list' { ;
			gen `person'_yrcrime2a`type'_`i' = `person'_any_`type'`i' if (keep_yr`i'_`type' == 1) ;
		} ;
		
		egen any_yrcrime2a`type'_`i' = rowmax(kid_yrcrime2a`type'_`i' sib*_yrcrime2a`type'_`i') ;

		foreach person in kid `sib_list' { ;
			drop 		`person'_yrcrime2a`type'_`i' ;
		} ; 
		
	} ;
} ;


summ any* ca* ;

gen byte n_post=(dobdd_run>0);
tab n_post, m;

rename keep_1997_2017_any tkeep_1997_2017_any ;
drop 
	first_name fssr_middlen last_name from_prwora from_name idu middle_name
	n_combpay_* n_onssi_* n_sibpayall_* 
	n_hhearns1996_* n_hhmbayr1996_* n_hhcombpay1996_* n_inchh1996_* 
	n_hhearns_* n_hhmbayr_* n_hhcombpay_* n_inchh_* earn* *days_in_* *event* *day_of_week* 
	keep_* i *sib2* *sib3* *sib4* *sib5* *sib6* *sib7* ;
rename tkeep_1997_2017_any keep_1997_2017_any ;

save "`crime'\prep2\crime_prworacombsib_prepforreg.dta", replace ;

capture log close;
